package org.dromara.common.translation.core.impl;

import cn.hutool.core.util.ObjectUtil;
import lombok.AllArgsConstructor;
import org.dromara.common.core.service.ComboboxService;
import org.dromara.common.json.utils.JsonUtils;
import org.dromara.common.translation.annotation.TranslationType;
import org.dromara.common.translation.constant.TransConstant;
import org.dromara.common.translation.core.TranslationInterface;

/**
 * 下拉框key,label缓存翻译实现
 *
 * @author Lus
 */
@AllArgsConstructor
@TranslationType(type = TransConstant.COMBOBOX_KEY_TO_LABEL)
public class ComboboxKeyTranslationImpl implements TranslationInterface<String> {

    private final ComboboxService comboboxService;

    @Override
    public String translation(Object key, String other) {
        if (ObjectUtil.isNull(key) || "null".equals(key.toString())) return null;
        if (key instanceof String && JsonUtils.isJson(key.toString())) {
            return comboboxService.queryLabels(other, key.toString());
        }
        return comboboxService.queryLabel(other, key.toString());
    }
}
